<?php

namespace Mobile\Controller;

use Home\Logic\UsersLogic;
use Think\Page;
use Think\Verify;

header('Content-Type:text/html;Charset=UTF-8');

class BankController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!session('?user')) {
            header("location:" . U('Home/User/login'));
        }
        //用户中心面包屑导航
        $navigate_user = navigate_user();
        $this->assign('navigate_user', $navigate_user);
    }

    /*
     * 资金变动明细
     */

    public function account_log() {
        $user = session('user');
        //获取账户资金记录
        $logic = new UsersLogic();
        $data = $logic->get_account_log($this->user_id, I('get.type'));
        $account_log = $data['result'];

        $this->assign('user', $user);
        $this->assign('account_log', $account_log);
        $this->assign('page', $data['show']);
        $this->assign('active', 'account');
        $this->display();
    }

    /*
     * 日收入明细
     */

    public function money_day_log() {
        $user = $this->user;
        $model = M('account_day_log');
        $count = $model->where(array('user_id' => $user['user_id']))->count();
        $page = new \Think\Page($count, 10);
        $list = $model->where(array('user_id' => $user['user_id']))->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->list = $list;
        $this->page = $page->show();
        $this->display();
    }

    /*
     * 日收入详情
     */

    public function money_day_log_detail() {
        $user = $this->user;
        $model = M('account_log');
        $map['user_id'] = $user['user_id'];
        $map['change_time'] = array('between', array($_GET['date'], $_GET['date'] + (24 * 3600)));
        $count = $model->where($map)->count();
        $page = new \Think\Page($count, 10);
        $list = $model->where($map)->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->list = $list;
        $this->page = $page->show();
        $this->display();
    }

    /*
     * 电子积分转账
     */

    public function user_money_fz() {
        $user = $this->user;
        $user = M('users')->where(array('user_id' => $user['user_id']))->field('user_money,zuce_money,user_id,secpwd,nickname,mobile,email,account')->find();
        if (IS_POST) {
            $zz_user = M('users')->where("mobile='{$_POST['zz_account']}' or email='{$_POST['zz_account']}' or account='{$_POST['zz_account']}'")->find();
            if (!$zz_user) {
                $this->error('该帐号不存在，请重新输入');
            }
            if ($user['secpwd'] != encrypt($_POST['secpwd'])) {
                $this->error('交易密码输入错误，请重新输入');
            }
            if ($_POST['money'] > $user['zuce_money']) {
                $this->error('电子积分余额不足,请重新输入');
            }
            if ($user['mobile'] == $_POST['zz_account'] || $user['email'] == $_POST['zz_account'] || $user['account'] == $_POST['zz_account']) {
                $this->error('不能自己转给自己');
            }
            $res = accountLog($user['user_id'],0, '-' . $_POST['money'],  0, 0, 0, 0,0, '转账给' . $_POST['zz_account']); // 扣掉转账所用的金额
            if ($res) {
                $result = accountLog($zz_user['user_id'], 0,'+' . $_POST['money'],  0, 0, 0, 0,0,$user['nickname'] . '转入');
                if ($result) {
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败');
                }
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->display();
        }
    }

    /*
     * 现金币转账
     */

    public function user_money_zz() {
            $user = $this->user;
            if (IS_POST) {
                $secpwd = I('post.secpwd', '');
                $type = I('post.type', '');
                $price = I('post.price', '');
                if ($secpwd == '') {
                    die('<script>alert("交易密码不能为空，请刷新页面后重试");history.go(-1);</script>');
                }
                if ($type == '') {
                    die('<script>alert("转账积分类型不能为空，请刷新页面后重试");history.go(-1);</script>');
                }
                if ($price == '') {
                    die('<script>alert("转账金额不能为空，请刷新页面后重试");history.go(-1);</script>');
                }

                if (encrypt($_POST['secpwd']) != $user['secpwd']) {
                    die('<script>alert("交易密码输入不正确");history.go(-1);</script>');
                }

//            if ($_POST['price'] < C('money_zz2_1') || $_POST['price'] % C('money_zz2_2') <> 0) {
//                die('<script>alert("转账金额最低' . C('money_zz2_1') . ' 并且是' . C('money_zz2_2') . '的倍数");history.go(-1);</script>');
//            }

                if ($_POST['price'] > $user['user_money']) {
                    die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
                }

//            $zzuser = M('user')->where(array('account' => $_POST['account']))->field('id')->find(); //查出对方信息
//            //判断是否存在
//            if (!$zzuser) {
//                die('<script>alert("该会员不存在");history.go(-1);</script>');
//            }
//            unset($_SESSION['first_leader']);
//            unset($_SESSION['next_accname']);
//            prevtd($user['first_leader']); //查出会员上级的帐号
//            nexttd($user['user_id']); //查出会员下级的帐号
//           // dump($_SESSION['next_accname']);exit;
//            if (!in_array($_POST['user_id'], $_SESSION['first_leader'])) {
//                if (!in_array($_POST['user_id'], $_SESSION['next_accname'])) {
//                    die('<script>alert("转账只能转直系网络会员");history.go(-1);</script>');
//                }
//            }

                if ($_POST['type'] == 'zuce_money') {
                    accountLog($user['user_id'], 0,'+' .  $_POST['price'], 0, 0, 0,'-' . $_POST['price'],0,'分享积分互换电子积分'); // 扣掉激活会员所用的金额
                } /*else if ($_POST['type'] == 'pay_points') {
                    accountLog($user['user_id'], '-' . $_POST['price'], 0, '+' . $_POST['price'], 0, 0, 0, 0, 0, '现金积分转复投积分');
                } */

                $this->success('操作成功');
            } else {
                $this->display();
            }
        }


    /*
     * 电子积分转库存积分 
     */

    public function dz_money_zh() {
            $user = $this->user;
            if (IS_POST) {
                $secpwd = I('post.secpwd', '');
                $type = I('post.type', '');
                $price = I('post.price', '');
                if ($secpwd == '') {
                    die('<script>alert("交易密码不能为空，请刷新页面后重试");history.go(-1);</script>');
                }
                if ($type == '') {
                    die('<script>alert("转账积分类型不能为空，请刷新页面后重试");history.go(-1);</script>');
                }
                if ($price == '') {
                    die('<script>alert("转账金额不能为空，请刷新页面后重试");history.go(-1);</script>');
                }

                if (encrypt($_POST['secpwd']) != $user['secpwd']) {
                    die('<script>alert("交易密码输入不正确");history.go(-1);</script>');
                }



                if ($_POST['price'] > $user['zuce_money']) {
                    die('<script>alert("电子积分不足,请充值后重试");history.go(-1);</script>');
                }

                if ($_POST['type'] == 'user_money') {
                    accountLog($user['user_id'], '+'.$_POST['price'],'-' . $_POST['price'], 0, 0, 0, 0, 0, '电子积分转库存积分');// 扣掉激活会员所用的金额

                }

                $this->success('操作成功');
            } else {
                $this->display();
            }
        }




    public function user_sj() {
        $user = $this->user;
        $user = M('users')->where(array('user_id' => $user['user_id']))->field('level,user_money,secpwd,user_id,first_leader')->find();
        $user_level = M('user_level');
        if (IS_POST) {
            if ($user['secpwd'] != encrypt($_POST['secpwd'])) {
                $this->error('交易密码输入不正确，请重新输入');
            }
            $gradea = $user_level->where(array('level_id' => $user['level']))->field('amount')->find(); // 算出原级别报单金额
            $gradeb = $user_level->where(array('level_id' => $_POST['grade']))->field('amount')->find(); // 算出升级后级别报单金额
            $gradec = $gradeb['amount'] - $gradea['amount'];
            if ($user['user_money'] < $gradec) {
                $this->error('现金币不足，请充值后再试');
            }
            $res = accountLog($user['user_id'],'-'.$gradec, 0,0,0,0,0, '原点补差价升级', 0);
            if ($res) {
                $data['user_id'] = $user['user_id']; // 会员id
                $data['y_grade'] = $user['level']; // 原级别
                $data['sj_grade'] = $_POST['grade']; // 升级后的级别
                $data['sj_time'] = time(); //申请时间
                $data['note'] = '补差价原点升级'; //升级备注
                $result = M('user_sj')->add($data);
                if ($result) {
                    clear_tjj($user['first_leader'], $gradec, '会员' . $user['nickname'] . '升级', 0);
                    M('users')->where(array('user_id' => $user['user_id']))->save(array('level' => $_POST['grade']));
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败，请刷新页面后重新操作');
                }
            } else {
                $this->error('操作失败，请刷新页面后重新操作');
            }
        } else {
            $this->user_level = $user_level->where(array('level_id' => array('gt', $user['level'])))->select();
            $sj_list = M('user_sj')->where(array('user_id' => $user['user_id']))->select();
            foreach ($sj_list as &$v) {
                $v['y_grade'] = $user_level->where(array('level_id' => $v['y_grade']))->getField('level_name');
                $v['sj_grade'] = $user_level->where(array('level_id' => $v['sj_grade']))->getField('level_name');
            }
            $this->sj_list = $sj_list;
            $this->display();
        }
    }
    /**
     * 申请提现记录
     */
    public function withdrawals() {
        $user = $this->user;
        $data = objarray_to_array(json_decode(tpCache('shopping.tk_time')));
        if (in_array(date('w'), $data)) {
            $starttime = strtotime(date('Y-m-d ')) + tpCache('shopping.tk_add_time') * 3600; // 开始时间
            $endtime = strtotime(date('Y-m-d')) + tpCache('shopping.tk_out_time') * 3600; // 结束时间
            if (time() > $endtime || $starttime > time()) {
                die('<script>alert("提现开放时间为' . tpCache('shopping.tk_add_time') . '-' . tpCache('shopping.tk_out_time') . ' 时");window.location.href="' . U('User/account') . '";</script>');
            }
        } else {
            die('<script>alert("提现开放时间暂未开启！");window.location.href="/";</script>');
        }

        if (IS_POST) {

            if ($user['is_tikuan'] == 0) {
                die('<script>alert("提现己关闭，请联系管理员");history.go(-1);</script>');
            }

            if (encrypt($_POST['secpwd']) != $user['secpwd']) {
                die('<script>alert("交易密码输入不正确");history.go(-1);</script>');
            }

            $data = I('post.');
            $data['user_id'] = $this->user_id;
            $data['account'] = $user['account'];
            $data['money'] = $_POST['money'] - $_POST['money'] * tpCache('shopping.tikuan_3') / 100;
            $data['poundage'] = $_POST['money'] * tpCache('shopping.tikuan_3') / 100;
            $data['create_time'] = time();
            $distribut_min = tpCache('shopping.tikuan_1'); // 最少提现额度
            if ($_POST['money'] < $distribut_min) {
                $this->error('每次最少提现额度' . $distribut_min);
                exit;
            }
            if ($data['money'] > $this->user['user_money']) {
                $this->error("你最多可提现{$this->user['user_money']}账户余额.");
                exit;
            }
            if ($_POST['money'] % tpCache('shopping.tikuan_2') != 0) {
                $this->error("提现金额必须是" . tpCache('shopping.tikuan_1') . "起并且是" . tpCache('shopping.tikuan_2') . "的倍数");
                exit;
            }

            if (M('withdrawals')->add($data)) {

                accountLog($user['user_id'], 0, 0, 0, 0, 0, '-' . $data['money'], 0,'提现申请'); // 扣掉激活会员所用的金额
                $this->success("已提交申请");
                exit;
            } else {
                $this->error('提交失败,联系客服!');
                exit;
            }
        }

        $where = " user_id = {$this->user_id}";
        $count = M('withdrawals')->where($where)->count();
        $page = new Page($count, 10);
        $show = $page->show();
        $list = M('withdrawals')->where($where)->order('id desc')->limit("{$page->firstRow},{$page->listRows}")->select();
        $this->assign('show', $show); // 赋值分页输出
        $this->assign('list', $list); // 下线
        $this->display();
    }

    public function apply_kucun() {
        $user = $this->user;
        if (IS_POST) {
            $data=I('post.');
            $data['add_time']=time();
            $data['user_id']=$user['user_id'];
            $data['order_sn'] = date('YmdHis').mt_rand(1000,9999);
            if ($data['pay_name']='电子积分') {
              $zuce_money=$this->user['zuce_money'];
              if ($zuce_money<$data['total_amount']) {
                $this->error('电子积分余额不足'); 
              }else{
                 accountLog($user['user_id'],0,'-'.$data['total_amount'],0,0,0,0,0,'电子积分购买产品');
              }

            }

          $r =M('bd_order')->add($data);
          if ($r) {

             $this->success('申请库存成功');
          }else{
            $this->error('申请失败');
          }
        } else {
            
            $this->display();
        }
    }

}
